import os
import hashlib
import shutil


def filemd5(filepath):
	with open(filepath, 'rb') as f:
		data = f.read()
	return hashlib.md5(data).hexdigest()


def rename_files(image_root):
	for root, dirs, files in os.walk(image_root):
		print(root)
		for name in files:
			image_path = os.path.join(root, name)
			md5_val = filemd5(image_path)
			ext = name.split('.')[-1]
			dst_path = os.path.join(root, '{}.{}'.format(md5_val, ext))
			if os.path.exists(dst_path): continue
			shutil.move(image_path, dst_path)


if __name__ == '__main__':
	rename_files('E:/Safety/classification/politics/crawlers/google/download_split/')
